//
//  UrlToVC.m
//  Westars
//
//  Created by westars on 15/7/6.
//  Copyright (c) 2015年 westars. All rights reserved.
//

#import "UrlToVC.h"
#import "KABMallMyShopVC.h"
#import "KABMallProDetailVC.h"
#import "KABWebViewVC.h"
#import "KABExhDetailVC.h"
#import "KABCosplayActivityVC.h"

@implementation UrlToVC

+ (UIViewController *)getVCfromUrlString:(NSString *)urlString
{
    //如果开头和结尾有空格，要添加保护，去掉空格
    urlString = [urlString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

    // 转换成小写
    urlString = [urlString lowercaseString];
    
    //解析字符串(http协议)
    if ([urlString hasPrefix:@"http://"] || [urlString hasPrefix:@"https://"]) {
        KABWebViewVC *vc = [[KABWebViewVC alloc] init];
        vc.requestUrl = [NSURL URLWithString:urlString];
        return vc;
    }
    
    if (![urlString hasPrefix:@"local://"]) {
        NSLog(@"要解析的字符串是%@,--无法解析", urlString);
        return nil;
    }
    
    //这里开始就是本地跳转
    UIViewController * toViewController = nil;
    
    NSRange range = [urlString rangeOfString:@"local://kabu/"];
    if ((range.location + range.length) >= urlString.length) {//添加保护
        return toViewController;
    }
    NSString * contentStr = [urlString substringFromIndex:(range.location + range.length)];
    NSArray * subVCStrArray = [contentStr componentsSeparatedByString:@"?"];
    if ([subVCStrArray[0] isEqualToString:@"myshop"]) {
        //跳转到我的店铺
        NSArray * topicIdArray = [subVCStrArray[1] componentsSeparatedByString:@"&"];
        NSRange tid = [topicIdArray[0] rangeOfString:@"shopownerid="];
        NSString * tidValueStr = nil;
        if ((tid.location + tid.length) < [topicIdArray[0] length]) {//添加保护
            tidValueStr = [topicIdArray[0] substringFromIndex:(tid.location + tid.length)];
            NSLog(@"店铺id等于%ld", (long)[tidValueStr integerValue]);
        }
        
        KABMallMyShopVC *shopVC = [[KABMallMyShopVC alloc] init];
        shopVC.shop_owner_id = [tidValueStr integerValue];
        toViewController = shopVC;
        
    } else if([subVCStrArray[0] isEqualToString:@"productdetail"]){
        //跳转到商品详情
        NSRange starStr = [subVCStrArray[1] rangeOfString:@"productid="];
        if ((starStr.location + starStr.length) >= [subVCStrArray[1] length]) {//添加保护
            return nil;
        }
        NSString * sidValueStr = [subVCStrArray[1] substringFromIndex:(starStr.location + starStr.length)];
        NSLog(@"商品详情id等于%ld", (long)[sidValueStr integerValue]);
        
        //创建一个明星专区的ViewController
        KABMallProDetailVC *detailVC = [[KABMallProDetailVC alloc] init];
        detailVC.productId = [sidValueStr integerValue];
        toViewController = detailVC;
        
    } else if([subVCStrArray[0] isEqualToString:@"exhibitiondetail"]){
        //跳转到商品详情
        NSRange starStr = [subVCStrArray[1] rangeOfString:@"exhibitionid="];
        if ((starStr.location + starStr.length) >= [subVCStrArray[1] length]) {//添加保护
            return nil;
        }
        NSString * sidValueStr = [subVCStrArray[1] substringFromIndex:(starStr.location + starStr.length)];
        NSLog(@"展会详情id等于%ld", (long)[sidValueStr integerValue]);
        
        //创建一个明星专区的ViewController
        KABExhDetailVC *detailVC = [[KABExhDetailVC alloc] init];
        detailVC.exhibition_id = [sidValueStr integerValue];
        toViewController = detailVC;
    } else if([subVCStrArray[0] isEqualToString:@"activityrank"]){
        //跳转到商品详情
        NSRange starStr = [subVCStrArray[1] rangeOfString:@"activityid="];
        if ((starStr.location + starStr.length) >= [subVCStrArray[1] length]) {//添加保护
            return nil;
        }
        NSString * sidValueStr = [subVCStrArray[1] substringFromIndex:(starStr.location + starStr.length)];
        NSLog(@"活动id等于%ld", (long)[sidValueStr integerValue]);
        
        //创建一个明星专区的ViewController
        KABCosplayActivityVC *detailVC = [[KABCosplayActivityVC alloc] init];
        detailVC.activityId = [sidValueStr integerValue];
        toViewController = detailVC;
    } else {
        NSLog(@"解析地址失败");
    }
    
    return toViewController;
}

@end
